package com.esisa.java.swing;

import java.awt.Color;
import java.awt.FlowLayout;

import javax.swing.BoxLayout;
import javax.swing.JPanel;

public class Form extends JPanel {
	private int labelWidth;
	private JPanel content;
	
	public Form() {
		this(100);
	}

	public Form(int labelWidth) { // LabelWidth pour la position des zone de texte 
		
		setLayout(new FlowLayout(FlowLayout.LEFT));
		this.labelWidth = labelWidth;
		
		content = new JPanel();
		content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));
		add(content);
	}
	
	public Form(Model model) {
		this(model.getLabelWidth()); 
		for (int i = 0; i < model.getFieldCount(); i++) { // nombre de champs
			addField(model.getFieldLabel(i), model.getFieldSize(i)); // les titre du label
		}
	}
	public void addField(LabeledTextField field) {
		content.add(field);
	}
	
	public void addField(String label, int size) {
		content.add(new LabeledTextField(label, size, labelWidth));
	}
	
	public void setValue(int index, String value) {
		LabeledTextField t = (LabeledTextField)content.getComponent(index);
		t.setValue(value);
	}
	
	public void setValues(String values[]) {
		for (int i = 0; i < values.length; i++) {
			setValue(i, values[i]);
		}
	}
	
	public String getValue(int index) {
		LabeledTextField t = (LabeledTextField)content.getComponent(index);
		return t.getValue();
	}
}
